package com.aa.activity;

import java.util.ArrayList;

import com.aa.R;

import android.R.integer;
import android.annotation.SuppressLint;
import android.app.Activity;
import android.os.Bundle;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.support.v4.view.ViewPager.OnPageChangeListener;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.RadioButton;
import android.widget.RadioButton;
import android.widget.TextView;
import android.widget.TextView;


public class AARecordActivity extends Activity {

	private RadioButton mrbtnName;
	private RadioButton mrbtnAge;
	private RadioButton mrbtnBirth;
	private RadioButton mrbtnBlood;
	private RadioButton mrbtnCharacteristic;
	private RadioButton mrbtnCharacter;
	private RadioButton mrbtnSkill;
	private RadioButton mrbtnCommunication;
	private RadioButton mrbtnHobby;
	private RadioButton mrbtnOther;
	private Button mbtnTitle;
	private TextView mtvCenterTitle;

	private ArrayList<View> mViews;
	private ViewPager mViewPager;
	private SlideViewAdapter mSlideViewAdapter;
	private int[] mResid = {R.string.aa_name,R.string.aa_birth,R.string.aa_age,R.string.aa_blood,
			R.string.aa_characteristic,R.string.aa_character,R.string.aa_skill,R.string.aa_communication,
			R.string.aa_hobby,R.string.aa_other};
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setContentView(R.layout.aa_record);
		initView();
		initListener();
		bindDate();
		//selectPosition(0);
		mrbtnName.setChecked(true);
	}
	
	private void bindDate(){
		mSlideViewAdapter = new SlideViewAdapter();
		mViewPager.setAdapter(mSlideViewAdapter);
		
	}
	private void initView(){
		mrbtnAge = (RadioButton)findViewById(R.id.rbtn_aa_age);
		mrbtnName = (RadioButton)findViewById(R.id.rbtn_aa_name);
		mrbtnBirth = (RadioButton)findViewById(R.id.rbtn_aa_birth);
		mrbtnBlood = (RadioButton)findViewById(R.id.rbtn_aa_blood);
		mrbtnCharacteristic = (RadioButton)findViewById(R.id.rbtn_aa_characteristic);
		mrbtnCharacter = (RadioButton)findViewById(R.id.rbtn_aa_character);
		mrbtnSkill = (RadioButton)findViewById(R.id.rbtn_aa_skill);
		mrbtnCommunication = (RadioButton)findViewById(R.id.rbtn_aa_communication);
		mrbtnHobby = (RadioButton)findViewById(R.id.rbtn_aa_hobby);
		mrbtnOther = (RadioButton)findViewById(R.id.rbtn_aa_other);
		
		mtvCenterTitle = (TextView)findViewById(R.id.tv_title_center);
		mtvCenterTitle.setText(R.string.aa_record);
		
		mbtnTitle = (Button)findViewById(R.id.btn_title_back);
	
		mbtnTitle.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				finish();
				
			}
		});
		
		mViewPager = (ViewPager)findViewById(R.id.vp_aa_say);	
		mViews = new ArrayList<View>();
				
		for (int i = 0; i < mResid.length; i++) {
			mViews.add(makeView(mResid[i]));
		}
		
		
	}
	
	private void initListener(){
		mrbtnAge.setOnClickListener(new OnrbtnClickListener());
		mrbtnName.setOnClickListener(new OnrbtnClickListener());
		mrbtnBirth.setOnClickListener(new OnrbtnClickListener());
		mrbtnBlood.setOnClickListener(new OnrbtnClickListener());
		mrbtnCharacteristic.setOnClickListener(new OnrbtnClickListener());
		mrbtnCharacter.setOnClickListener(new OnrbtnClickListener());
		mrbtnSkill.setOnClickListener(new OnrbtnClickListener());
		mrbtnCommunication.setOnClickListener(new OnrbtnClickListener());
		mrbtnHobby.setOnClickListener(new OnrbtnClickListener());
		mrbtnOther.setOnClickListener(new OnrbtnClickListener());
		mViewPager.setOnPageChangeListener(new OnViewPagerChangeListener());
	}
	
	private void cleanAllTextColor(){
		mrbtnAge.setTextColor(0xff000000);
		mrbtnName.setTextColor(0xff000000);
		mrbtnBirth.setTextColor(0xff000000);
		mrbtnBlood.setTextColor(0xff000000);
		mrbtnCharacteristic.setTextColor(0xff000000);
		mrbtnCharacter.setTextColor(0xff000000);
		mrbtnSkill.setTextColor(0xff000000);
		mrbtnCommunication.setTextColor(0xff000000);
		mrbtnHobby.setTextColor(0xff000000);
		mrbtnOther.setTextColor(0xff000000);		
		
	}
	private void setAllRadioButtonFalse(){
		mrbtnAge.setChecked(false);
		mrbtnName.setChecked(false);
		mrbtnBirth.setChecked(false);
		mrbtnBlood.setChecked(false);
		mrbtnCharacteristic.setChecked(false);
		mrbtnCharacter.setChecked(false);
		mrbtnSkill.setChecked(false);
		mrbtnCommunication.setChecked(false);
		mrbtnHobby.setChecked(false);
		mrbtnOther.setChecked(false);
	}
	private void selectPosition(int index){	
		setAllRadioButtonFalse();
		cleanAllTextColor();
		switch (index) {
		case 0:
			mrbtnName.setChecked(true);
			mrbtnName.setTextColor(0xff48cdff);
			break;
		case 1:
			mrbtnBirth.setChecked(true);
			mrbtnBirth.setTextColor(0xff48cdff);
			break;
		case 2:
			mrbtnAge.setChecked(true);
			mrbtnAge.setTextColor(0xff48cdff);
			break;
		case 3:
			mrbtnBlood.setChecked(true);
			mrbtnBlood.setTextColor(0xff48cdff);
			break;
		case 4:
			mrbtnCharacteristic.setChecked(true);
			mrbtnCharacteristic.setTextColor(0xff48cdff);
			break;
		case 5:
			mrbtnCharacter.setChecked(true);
			mrbtnCharacter.setTextColor(0xff48cdff);
			break;
		case 6:
			mrbtnSkill.setChecked(true);
			mrbtnSkill.setTextColor(0xff48cdff);
			break;
		case 7:
			mrbtnCommunication.setChecked(true);
			mrbtnCommunication.setTextColor(0xff48cdff);
			break;
		case 8:
			mrbtnHobby.setChecked(true);
			mrbtnHobby.setTextColor(0xff48cdff);
			break;
		case 9:
			mrbtnOther.setChecked(true);
			mrbtnOther.setTextColor(0xff48cdff);
			break;
		default:
			break;
		}
	}
	
	private class OnrbtnClickListener implements OnClickListener{

		
		@Override
		public void onClick(View v) {
			cleanAllTextColor();
			setAllRadioButtonFalse();
			switch (v.getId()) {
			case R.id.rbtn_aa_age:
				mrbtnAge.setTextColor(0xff48cdff);
				mViewPager.setCurrentItem(2);
				break;
			case R.id.rbtn_aa_name:
				mrbtnName.setTextColor(0xff48cdff);
				mViewPager.setCurrentItem(0);
				break;
			case R.id.rbtn_aa_birth:
				mrbtnBirth.setTextColor(0xff48cdff);
				mViewPager.setCurrentItem(1);
				break;
			case R.id.rbtn_aa_blood:
				mrbtnBlood.setTextColor(0xff48cdff);
				mViewPager.setCurrentItem(3);
				break;
			case R.id.rbtn_aa_characteristic:
				mrbtnCharacteristic.setTextColor(0xff48cdff);
				mViewPager.setCurrentItem(4);
				break;
			case R.id.rbtn_aa_character:
				mrbtnCharacter.setTextColor(0xff48cdff);
				mViewPager.setCurrentItem(5);
				break;
			case R.id.rbtn_aa_skill:
				mrbtnSkill.setTextColor(0xff48cdff);
				mViewPager.setCurrentItem(6);
				break;
			case R.id.rbtn_aa_communication:
				mrbtnCommunication.setTextColor(0xff48cdff);
				mViewPager.setCurrentItem(7);
				break;
			case R.id.rbtn_aa_hobby:
				mrbtnHobby.setTextColor(0xff48cdff);
				mViewPager.setCurrentItem(8);
				break;
			case R.id.rbtn_aa_other:
				mrbtnOther.setTextColor(0xff48cdff);
				mViewPager.setCurrentItem(9);
				break;
			
			}
		}
		
	}
	
	private class SlideViewAdapter extends PagerAdapter{

		@Override
		public int getCount() {
			
			return mViews.size();
		}

		@Override
		public boolean isViewFromObject(View arg0, Object arg1) {
			
			return arg0 == arg1;
		}
		
		@Override
		public Object instantiateItem(View container, int position) {
			((ViewPager) container).addView(mViews.get(position));
			return mViews.get(position);
		}

		@Override
		public void destroyItem(View container, int position, Object object) {
			// TODO Auto-generated method stub
			((ViewPager) container).removeView(mViews.get(position));  
			
		}		
	}
	
	private class OnViewPagerChangeListener implements OnPageChangeListener{

		@Override
		public void onPageScrollStateChanged(int arg0) {						
		}

		@Override
		public void onPageScrolled(int arg0, float arg1, int arg2) {					
		}

		@Override
		public void onPageSelected(int arg0) {
			selectPosition(arg0);			
		}
		
	}
	
	private View makeView(int resid){
		LinearLayout _LinearLayout = new LinearLayout(this);
		LinearLayout.LayoutParams _LayoutParams = new LinearLayout.LayoutParams(
				LinearLayout.LayoutParams.MATCH_PARENT, 
				LinearLayout.LayoutParams.WRAP_CONTENT,
				1);
		TextView rbtn =  new TextView(this);
		rbtn.setText(resid);
		rbtn.setTextSize(20);
		rbtn.setTextColor(0xff000000);
		_LinearLayout.addView(rbtn,_LayoutParams);
		return _LinearLayout;
		
	}
	

}
